package com.cloudtcm.controller.user;

import com.cloudtcm.constant.JwtClaimsConstant;
import com.cloudtcm.dto.UserDTO;
import com.cloudtcm.dto.UserLoginDTO;
import com.cloudtcm.entity.User;
import com.cloudtcm.properties.JwtProperties;
import com.cloudtcm.result.Result;
import com.cloudtcm.serviece.UserService;
import com.cloudtcm.utils.JwtUtil;
import com.cloudtcm.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Api(tags = "用户相关接口")
@CrossOrigin("*")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 用户登录
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
        log.info("用户登录,{}",userLoginDTO);

        User user = userService.login(userLoginDTO);

        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getSecretKey(),
                jwtProperties.getTtl(),
                claims);

        UserLoginVO  userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .userName(user.getUsername())
                .image(user.getImage())
                .phone(user.getPhone())
                .createTime(user.getCreateTime())
                .height(user.getHeight())
                .weight(user.getWeight())
                .comment(user.getComment())
                .token(token)
                .build();
        return Result.success(userLoginVO);
    }

    /**
     * 用户注册
     * @param userDTO
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("用户注册")
    public Result register(@RequestBody UserDTO userDTO){
        log.info("用户注册,{}", userDTO);

        userService.save(userDTO);
        return Result.success();
    }

    /**
     * 更新信息
     * @param userDTO
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("更新用户信息")
    public Result update(@RequestBody UserDTO userDTO){
        log.info("更新员工信息:{}",userDTO);
        userService.update(userDTO);
        return Result.success();
    }
<<<<<<< HEAD
=======


>>>>>>> c293085240bf5ef30a1e2a3a3198549eadf17f99
}
